package com.youlin.service.impl;

import com.youlin.dto.BaseResDto;
import com.youlin.dto.Status;
import com.youlin.dto.req.PhotoReqDto;
import com.youlin.entities.BygPhoto;
import com.youlin.mapper.BygPhotoMapper;
import com.youlin.service.BygPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlin.untils.MyBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BygPhotoServiceImpl extends ServiceImpl<BygPhotoMapper, BygPhoto> implements BygPhotoService {

    @Override
    @Transactional
    public BaseResDto editDmPhotos(List<PhotoReqDto> photos) {
        if (photos.parallelStream().anyMatch(photo -> MyBeanUtil.isParamError(photo)))
            return new BaseResDto(Status.PARAMETERERROR);
        if (photos.parallelStream().anyMatch(p ->
                ! Arrays.asList("520*700", "625*415", "530*620").contains(p.getWh())))
            return new BaseResDto(Status.PHOTOSIZEERROR);
        long isFamilyCount = photos.parallelStream().filter(p -> p.getIsFamily()).count();
        if (isFamilyCount != 1) return new BaseResDto(Status.ONEISFAMILY);
        List<BygPhoto> saveOrUpdateList = photos.parallelStream()
                .map(p -> MyBeanUtil.copyProperties(p, BygPhoto.class))
                .collect(Collectors.toList());
        if (saveOrUpdateBatch(saveOrUpdateList))
            return new BaseResDto(Status.SUCCESS);
        return new BaseResDto(Status.ERROR);
    }

    @Override
    @Transactional
    public BaseResDto delDmPhoto(Integer photoId) {
        BygPhoto photo = getById(photoId);
        if (photo.getIsFamily()) return new BaseResDto(Status.DELFPERROR);
        if (removeById(photoId)) return new BaseResDto(Status.SUCCESS);
        return new BaseResDto(Status.ERROR);
    }

}
